package com.dingyx.ps.handler;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.dingyx.ps.model.ReturnT;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.validation.ConstraintViolationException;
import org.springframework.validation.ObjectError;
import org.springframework.util.CollectionUtils;
import javax.validation.ConstraintViolation;

import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Set;

/**
 * @author dingyuxin
 *
 */
@Slf4j
@RestControllerAdvice(basePackages = {"com.dingyx.ps"})
public class GlobalExceptionHandler {

    /**
     * 用来处理bean validation异常
     * @param ex
     * @return
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public ReturnT resolveConstraintViolationException(ConstraintViolationException ex){
        log.info("ConstraintViolationException-异常服务-->{}", ex.getMessage());
        ReturnT result = ReturnT.ERROR();
        Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
        if(!CollectionUtils.isEmpty(constraintViolations)){
            StringBuilder msgBuilder = new StringBuilder();
            for(ConstraintViolation constraintViolation :constraintViolations){
                msgBuilder.append(constraintViolation.getMessage()).append(",");
            }
            String errorMessage = msgBuilder.toString();
            if(errorMessage.length()>1){
                errorMessage = errorMessage.substring(0,errorMessage.length()-1);
            }
            result.setMsg(errorMessage);
            return result;
        }
        result.setMsg(ex.getMessage());
        return result;
    }

    /**
     * 方法不支持异常
     * @param ex
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ReturnT resolveMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        log.info("MethodArgumentNotValidException-异常服务-->{}", ex.getMessage());
        ReturnT result = ReturnT.ERROR();
        List<ObjectError> objectErrors = ex.getBindingResult().getAllErrors();
        if(!CollectionUtils.isEmpty(objectErrors)) {
            StringBuilder msgBuilder = new StringBuilder();
            for (ObjectError objectError : objectErrors) {
                msgBuilder.append(objectError.getDefaultMessage()).append(",");
            }
            String errorMessage = msgBuilder.toString();
            if (errorMessage.length() > 1) {
                errorMessage = errorMessage.substring(0, errorMessage.length() - 1);
            }
            result.setMsg(errorMessage);
            return result;
        }
        result.setMsg(ex.getMessage());
        return result;
    }

    /**
     * 默认异常处理
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ReturnT resolveDefaultException(Exception ex){
        log.info("Default Exception - {}", ExceptionUtil.getMessage(ex));
        return ReturnT.ERROR("服务错误 请稍后重试");
    }
}